package com.turnengine.client.local.error.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.error.bean.ILocalTrace;
import com.turnengine.client.local.error.bean.LocalTraceSerializer;
import java.io.IOException;

/**
 * The Get Local Trace Data Serializer.
 */
public class GetLocalTraceDataSerializer extends AbstractCommandDataSerializer<ILocalTrace> implements IGetLocalTraceDataSerializer {

	/**
	 * Creates a new Get Local Trace Data Serializer.
	 */
	public GetLocalTraceDataSerializer() {
		super(GetLocalTrace.COMMAND_ID);
	}

	@Override
	public ILocalTrace readReturnValue(IDataReader reader) throws IOException {
		ILocalTrace value = new LocalTraceSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, ILocalTrace value) throws IOException {
		new LocalTraceSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<ILocalTrace> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		String id = reader.readString(true);

		// Create object
		GetLocalTrace object = new GetLocalTrace();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setId(id);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<ILocalTrace> argument) throws IOException {
		GetLocalTrace object = (GetLocalTrace) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		String id = object.getId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeString(id, true);
	}

	@Override
	public IExecutableCommandResponse<ILocalTrace> newResponse() {
		return new GetLocalTraceResponse();
	}
}
